package com.bjsxt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Operate01 {
    private List<Person> list;
    public Operate01(){
        this.list = new ArrayList<>();
    }
    //用户添加记录业务逻辑控制
    public void addLogic(){
        Menu01 menu01 = new Menu01();
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        while (true){
            menu01.addMenu();
            int item = telNoteRegex01.menuItemValidate(1,3);
            switch (item){
                case 1:this.addOperation();break;
                case 2:this.showAll();break;
                //return结束了while循环，addLogic方法就会
                //跟着结束，就会返回上一级
                case 3:return;
            }
        }

    }
    //用户查询记录业务逻辑控制
    public void searchLogic(){
        Menu01 menu01 = new Menu01();
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        while (true){
            menu01.searchMenu();
            int item = telNoteRegex01.menuItemValidate(1,7);
            switch (item){
                case 1:this.searchBYName();break;
                case 2:this.searchByAge();break;
                case 3:this.searchBySex();break;
                case 4:this.searchByTelNum();break;
                case 5:this.searchByAddress();break;
                case 6:this.showAll();break;
                case 7:return;
            }
        }

    }
    //修改记录业务逻辑
    public void modifyLogic(){
        Menu01 menu01 = new Menu01();
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        while (true){
            menu01.modifyMenu();
            int item = telNoteRegex01.menuItemValidate(1,3);
            switch (item){
                case 1: this.showAll();break;
                case 2: this.modifyOperation();break;
                case 3: return;
            }
        }

    }
    //删除记录业务逻辑控制
    public void deleteLogic(){
        Menu01 menu01 = new Menu01();
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        while (true){
            menu01.deleteMenu();
            int item = telNoteRegex01.menuItemValidate(1,4);
            switch (item){
                case 1: this.showAll();break;
                case 2: this.deleteOperation();break;
                case 3:this.deleteAllOperation();break;
                case 4: return;
            }
        }

    }
    //排序记录业务逻辑控制
    public void orderLogic(){
        Menu01 menu01 = new Menu01();
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        while (true){
            menu01.orderMenu();
            int item = telNoteRegex01.menuItemValidate(1,5);
            switch (item){
                case 1: this.orderName();break;
                case 2: this.orderAge();break;
                case 3:this.orderSex();break;
                case 4:this.showAll();break;
                case 5: return;
            }
        }

    }
    //添加新记录信息
    public void addOperation(){
       TelNoteRegex telNoteRegex = new TelNoteRegex();
       String name = telNoteRegex.nameValidate();
       String age = telNoteRegex.ageValidate();
       String sex = telNoteRegex.sexValidate();
       String telNum = telNoteRegex.telNumValidate();
       String address = telNoteRegex.addressValidate();
       Person person = new Person(name,age,sex,telNum,address);
     // person.setId(this.list.size()+1);  在添加前要+1，因为list.size()在没有时是0
       //添加到容器
        this.list.add(person);
        person.setId(this.list.size());
    }
    //查询全部记录
    public void showAll(){
       if(this.list.size()==0){
           System.out.println("没有记录");
           return;
       }
       for(int i=0;i<this.list.size();i++){
           System.out.println(this.list.get(i));
       }
    }
    //按姓名查询记录
    public void searchBYName(){
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        String name = telNoteRegex01.nameValidate();
        boolean flag=true;
        for(int i=0;i<this.list.size();i++){
            if(name.equals(this.list.get(i).getName())){
                System.out.println(this.list.get(i));
                flag = false;
            }
        }
        if(flag){
            System.out.println("没有记录");
        }
    }
    //按年龄查询记录
    public void searchByAge(){
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        String age = telNoteRegex01.ageValidate();
        boolean flag=true;
        for(int i=0;i<this.list.size();i++){
            if(age.equals(this.list.get(i).getAge())){
                System.out.println(this.list.get(i));
                flag = false;
            }
        }
        if(flag){
            System.out.println("没有记录");
        }

    }
    //按性别查询记录
    public void searchBySex(){
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        String sex = telNoteRegex01.sexValidate();
        boolean flag=true;
        for(int i=0;i<this.list.size();i++){
            if(sex.equalsIgnoreCase(this.list.get(i).getSex())){
                System.out.println(this.list.get(i));
                flag = false;
            }
        }
        if(flag){
            System.out.println("没有记录");
        }



    }
    //按电话号码查询记录
    public void searchByTelNum(){
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        String telNum = telNoteRegex01.telNumValidate();
        boolean flag=true;
        for(int i=0;i<this.list.size();i++){
            if(telNum.equals(this.list.get(i).getTelNum())){
                System.out.println(this.list.get(i));
                flag = false;
            }
        }
        if(flag){
            System.out.println("没有记录");
        }


    }
    //按地址查询记录
    public void searchByAddress(){
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        String address = telNoteRegex01.addressValidate();
        boolean flag = true;
        for (int i=0;i<this.list.size();i++){
            if(address.equals(this.list.get(i).getAddress())){
                System.out.println(this.list.get(i));
                flag = false;
            }
        }
        if(flag){
            System.out.println("没有此人记录");
        }
    }
    //修改指定记录
    public void  modifyOperation(){
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        Menu01 menu01 = new Menu01();
        System.out.println("请输入记录对象");
        int itemNum = telNoteRegex01.menuItemValidate(1, list.size());
        menu01.subModifyMenu();
        int menuNum = telNoteRegex01.menuItemValidate(1,6);
       switch (menuNum){
           case 1: String name = telNoteRegex01.nameValidate();(this.list.get(itemNum-1)).setName(name);break;
           case 2: String age  = telNoteRegex01.ageValidate();(this.list.get(itemNum-1)).setAge(age);break;
           case 3: String sex = telNoteRegex01.sexValidate();(this.list.get(itemNum-1)).setSex(sex);break;
           case 4: String tulNum = telNoteRegex01.telNumValidate();(this.list.get(itemNum-1)).setTelNum(tulNum);break;
           case 5: String adress = telNoteRegex01.addressValidate();(this.list.get(itemNum-1)).setAddress(adress);break;
           case 6: return;
       }


    }
    //删除指定记录
    public void deleteOperation(){
        TelNoteRegex01 telNoteRegex01 = new TelNoteRegex01();
        System.out.println("请输入删除的记录序号");
        int itemNum = telNoteRegex01.menuItemValidate(1, list.size());
        this.list.remove(itemNum-1);
        for(int i=0;i<this.list.size();i++){
            (this.list.get(i)).setId(i+1);
        }
        System.out.println("删除成功，请继续操作");
    }
    //删除全部记录
    public void deleteAllOperation(){
        this.list.clear();
        System.out.println("电话本内容已清空，请继续操作");
    }
    //按用户姓名排序记录
    public void orderName(){

        Collections.sort(this.list,new OrderByName());
        for(int i=0;i<this.list.size();i++){
            (this.list.get(i)).setId(i+1);
        }
    }
    //按用户年龄排序记录
    public void orderAge(){
        Collections.sort(this.list,new OrderByAge());
        for(int i=0;i<this.list.size();i++){
            (this.list.get(i)).setId(i+1);
        }

    }
    //按用户性别排序记录
    public void orderSex(){
        Collections.sort(this.list,new OrderBySex());
        for(int i=0;i<this.list.size();i++){
            (this.list.get(i)).setId(i+1);
        }
    }

    /*排序器只能对在电话本中所存储的记录进行排序，不具备通用性，所以定义
     * 成Operate 的内部类*/
    //按姓名排序的比较器
    class OrderByName implements Comparator<Person> {
        //implement :执行  实施

        @Override
        public int compare(Person o1, Person o2) {

            return o1.getName().compareTo(o2.getName());
        }
    }
    //按年龄排序的比较器
    class OrderByAge implements Comparator<Person>{
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getAge().compareTo(o2.getAge());
        }
    }
    //按性别排序的比较器
    class OrderBySex implements Comparator<Person>{
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getSex().compareTo(o2.getSex());
        }
    }
}
